// 1. 计算一致字符串
const allowed :string='ab'
const words :string []=['ad','bd','aaab','baa','badab']
function findStr(allowed:string,words:string[]):number{
    let count:number =0;
    for(let i = 0;i < words.length;i++) {
        let arr:string[] =words[i].split('');
        if(arr.every(item=>allowed.includes(item))){
            count++
        }
    }
    return count
}
console.log(findStr(allowed,words));

// 2. 获取url及封装深拷贝方法
function searchUrl(url:string){
    const parmaStr =url.split('?')[1];
    const parArr=parmaStr.split('&');
    let obj={};
    parArr.forEach((item:string)=>{
        const key=item.split('=')[0];
        const value =item.split('=')[1];
        const newObj={
            [key]:value
        }
        // 合并对象
        obj=Object.assign(Object.assign({}, obj), newObj)
    })
    return obj
}
console.log(searchUrl(' http: //example.com/index.html?a=1&b=2&c=3&d=true'));


// 3. 买卖股票的最佳时机
function func (arr:number[]):number{
    let sum =0;
    for(let i = 0;i < arr.length;i++) {
        let diffNum =arr[i+1]-arr[1]
        if(diffNum>0){
            sum+=diffNum
        }
    }
    return sum
}
console.log(func([7,1,5,3,6,4]));

// 4. 找只出现一次的元素
function single(nums:number[]){
    let num=-1
    for(let i = 0;i <nums.length ;i++) {
        if(nums.indexOf(nums[i])==nums.lastIndexOf(nums[i])){
            num=nums[i]
        }
    }
    return num
}
console.log(single([1,2,3,1,3]));


// 5. 多维转一维
let arr:Array<number | string |Array<number |string |Array<number | string>>> =[1,'张',[2,"三",[3,'李']]]

let newArr:Array<number|string|Array<number|string |Array<number |string>>>=[]

function MyFlat (arr:Array<number|string|Array<number|string |Array<number |string>>>):any[]{
    arr.forEach((item:any)=>{
        if(Array.isArray(item)){
            MyFlat(item)
        }else{
            newArr.push(item)
        }
    })
    return newArr
}
console.log(MyFlat(arr));
